/*
 * Created by JFormDesigner on Thu Nov 19 20:48:38 CST 2020
 */

package com.wf.atm.server;


import com.wf.atm.pojo.vo.RankVo;
import com.wf.atm.service.Bank;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author 11
 */
public class RankPanel extends JPanel {

    Bank bank;

    public RankPanel() {
        this.bank = Bank.getInstance();
        initComponents();
    }


    //点击返回按钮，跳转至ServerPanel
    private void backButtonActionPerformed(ActionEvent e) {
        CardLayout card = (CardLayout)this.getParent().getLayout();
        card.show(getParent(),"server");
    }

    //查看总资产
    private void totalBalanceButtonActionPerformed(ActionEvent e) {
        //执行bank业务
        double balance = bank.sumAllBalance();
        label.setText("总资产"+balance+"元");
    }

    //查看总透支额度
    private void totalCeilingButtonActionPerformed(ActionEvent e) {
        double ceiling = bank.sumAllCeiling();
        label.setText("总透支额度"+ceiling+"元");
    }

    //查看贷款额度
    private void totalLoanButtonActionPerformed(ActionEvent e) {
        double loan = bank.sumAllLoan();
        label.setText("总贷款额度"+loan+"元");
    }

    //查看总资产排序
    private void rankButtonActionPerformed(ActionEvent e) {


        Set<RankVo> rank = bank.assetRanking();
        //将TreeSet<RankVO>集合 渲染到表格中
        //表格
        JTable jTable = new JTable();
        //标题
        String[] titles = {"序号","姓名","身份证号","总资产"};
        //数据
        Object[][] datas = new Object[rank.size()][titles.length];
        int count = 0;//计数
        for (RankVo rankVO : rank) {
            datas[count++] = new Object[]{count,rankVO.getName(),rankVO.getPersonId(),rankVO.getBalance()};
        }
        //渲染到表格中
        DefaultTableModel model = (DefaultTableModel)jTable.getModel();
        model.setDataVector(datas,titles);
        scrollPane.setViewportView(jTable);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        totalBalanceButton = new JButton();
        totalCeilingButton = new JButton();
        totalLoanButton = new JButton();
        rankButton = new JButton();
        backButton = new JButton();
        label = new JLabel();
        scrollPane = new JScrollPane();
        table = new JTable();

        //======== this ========
        setLayout(null);

        //---- totalBalanceButton ----
        totalBalanceButton.setText("查看总余额");
        totalBalanceButton.addActionListener(e -> totalBalanceButtonActionPerformed(e));
        add(totalBalanceButton);
        totalBalanceButton.setBounds(20, 50, 105, 30);

        //---- totalCeilingButton ----
        totalCeilingButton.setText("透支总额");
        totalCeilingButton.addActionListener(e -> totalCeilingButtonActionPerformed(e));
        add(totalCeilingButton);
        totalCeilingButton.setBounds(20, 120, 105, 30);

        //---- totalLoanButton ----
        totalLoanButton.setText("贷款总额");

        totalLoanButton.addActionListener(e -> totalLoanButtonActionPerformed(e));
        add(totalLoanButton);
        totalLoanButton.setBounds(20, 190, 105, 30);

        //---- rankButton ----
        rankButton.setText("总资产排名");
        rankButton.addActionListener(e -> rankButtonActionPerformed(e));
        add(rankButton);
        rankButton.setBounds(20, 255, 105, 30);

        //---- backButton ----
        backButton.setText("返回");
        backButton.addActionListener(e -> backButtonActionPerformed(e));
        add(backButton);
        backButton.setBounds(20, 325, 105, 30);

        //---- label ----
        label.setHorizontalAlignment(SwingConstants.CENTER);
        add(label);
        label.setBounds(190, 55, 230, 300);

        //======== scrollPane ========
        {
            scrollPane.setViewportView(table);
        }
        add(scrollPane);
        scrollPane.setBounds(190, 55, 230, 300);

        setPreferredSize(new Dimension(450, 460));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JButton totalBalanceButton;
    private JButton totalCeilingButton;
    private JButton totalLoanButton;
    private JButton rankButton;
    private JButton backButton;
    private JLabel label;
    private JScrollPane scrollPane;
    private JTable table;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
